package com.chencs.biz.special.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.model.service.ModelService;
import com.chencs.biz.special.service.SpecialService;
import com.chencs.vo.biz.model.ModeModel;
import com.chencs.vo.biz.product.ProductModel;
import com.chencs.vo.biz.special.SpecialModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by fangzehua on 2017/7/8.
 */

@RestController
@RequestMapping("/special")
public class SpecialController {

    @Autowired
    private SpecialService specialService;


    @RequestMapping(value = "/getSpecialList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getSpecialList(){
        List<SpecialModel> list = specialService.getSpecialList();
        if(!list.isEmpty()){
            return JSON.toJSONString(list);
        }else{
            return ResponseOptUtil.commonMsgError("获取专题列表失败");
        }
    }

    @RequestMapping(value = "/findSpecialById", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findSpecialById(@RequestBody String id){
        SpecialModel model = specialService.findSpecial(id);
        if(model!=null){
            return JSON.toJSONString(model);
        }else{
            return ResponseOptUtil.commonMsgError("获取专题失败");
        }
    }

    @RequestMapping(value = "/insertSpecial", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String insertSpecial(@RequestBody SpecialModel modeModel){
        try {
            specialService.insertSpecial(modeModel);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("添加专题失败");
        }
    }

    @RequestMapping(value = "/updateSpecial", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateSpecial(@RequestBody SpecialModel modeModel){
        try {
            specialService.updateSpecial(modeModel);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("修改专题失败");
        }
    }

    @RequestMapping(value = "/getSpecialProductById", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateSpecial(@RequestBody String  sid){
        List<ProductModel> list = null;
        try {
            list =  specialService.findSpecialProBySid(sid);
            return JSON.toJSONString(list);
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("修改专题失败");
        }
    }


    @RequestMapping(value = "/delSpecial", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String delSpecial(@RequestBody String  id){
        try {
            specialService.delSpecial(id);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("刪除专题失败");
        }
    }


    @RequestMapping(value = "/delSpecialProduct", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String delSpecialProduct(@RequestBody String  id){
        try {
            specialService.delSpecialProduct(id);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("商品删除失败");
        }
    }

    @RequestMapping(value = "/insertSpecialProduct", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String insertSpecialProduct(@RequestBody String  id){
        try {
            specialService.insertSpecialProduct(id);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            return ResponseOptUtil.commonMsgError("商品删除失败");
        }
    }


}
